001 /*
002 * Copyright 2005 Stephen McConnell.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
013 * implied.
014 *
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package net.dpml.util;
020
021 import net.dpml.lang.PID;
022
023 import java.util.logging.Formatter;
024 import java.util.logging.LogRecord;
025
026 /**
027 * Logging message formatter that includes the category in the logging statement.
028 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
029 * @version 1.0.1
030 */
031 public class StandardFormatter extends Formatter
032 {
033 private static final PID ID = new PID();
034
035 private static final String SEPARATOR = System.getProperty( "line.separator" );
036
037 /**
038 * Format a LogRecord using a style appropriate for console messages. The
039 * the log record message will be checked for a process identifier that is
040 * prepended to the raw message using the convention "$[" + PID + "] ". If
041 * a process id is resolved the value is assigned to the process block and
042 * the raw message is trimmed. The resulting formatted message is presented
043 * in the form "[PID ] [LEVEL ] (log.category): the message".
044 *
045 * @param record the log record to be formatted.
046 * @return a formatted log record
047 */
048 public synchronized String format( LogRecord record )
049 {
050 if( null == record )
051 {
052 return "";
053 }
054 String process = getProcessHeader( record );
055 StringBuffer buffer = new StringBuffer( process );
056 String header = getLogHeader( record );
057 buffer.append( header );
058 if( null != record.getLoggerName() )
059 {
060 buffer.append( "(" + record.getLoggerName() + "): " );
061 }
062 else
063 {
064 buffer.append( "() " );
065 }
066 String message = getFormattedMessage( record );
067 buffer.append( message );
068 buffer.append( SEPARATOR );
069 if( record.getThrown() != null )
070 {
071 Throwable cause = record.getThrown();
072 String error = ExceptionHelper.packException( cause, true );
073 buffer.append( error );
074 }
075 return buffer.toString();
076 }
077
078 private String getFormattedMessage( LogRecord record )
079 {
080 String message = formatMessage( record );
081 if( ( null != message ) && message.startsWith( "$[" ) )
082 {
083 int n = message.indexOf( "] " );
084 return message.substring( n + 2 );
085 }
086 else
087 {
088 return message;
089 }
090 }
091
092 private String getLogHeader( LogRecord record )
093 {
094 StringBuffer buffer = new StringBuffer();
095 buffer.append( "[" );
096 buffer.append( record.getLevel().getLocalizedName() );
097 buffer.append( " " );
098 String tag = buffer.toString();
099 return tag.substring( 0, LEVEL_HEADER_WIDTH ) + "] ";
100 }
101
102 private String getProcessHeader( LogRecord record )
103 {
104 StringBuffer buffer = new StringBuffer();
105 buffer.append( "[" );
106 String message = record.getMessage();
107 if( ( null != message ) && ( message.startsWith( "$[" ) ) )
108 {
109 int n = message.indexOf( "] " );
110 String id = message.substring( 2, n );
111 buffer.append( id );
112 }
113 else
114 {
115 buffer.append( ID.getValue() );
116 }
117 buffer.append( " " );
118 String tag = buffer.toString();
119 return tag.substring( 0, PROCESS_HEADER_WIDTH ) + "] ";
120 }
121
122 private static final int LEVEL_HEADER_WIDTH = 8;
123 private static final int PROCESS_HEADER_WIDTH = 6;
124 }